package com.fenghongzhang.module_main.ui.post

import android.graphics.Rect
import androidx.fragment.app.viewModels
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fenghongzhang.lib_core.base.BaseFragment
import com.fenghongzhang.lib_core.base.observeWithLifecycle
import com.fenghongzhang.lib_core.state.UILoader
import com.fenghongzhang.lib_core.utils.ToastUtils
import com.fenghongzhang.module_main.R
import com.fenghongzhang.module_main.databinding.FragmentRecommendBinding
import com.fenghongzhang.module_main.databinding.LayoutRefreshRecyclerviewBinding
import com.jczy.cyclone.mmkv.AuthMMKV
import com.jczy.cyclone.widget.photoview.PhotoViewerDialog
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

@AndroidEntryPoint
class PostListFragment : BaseFragment<PostViewModel, FragmentRecommendBinding>() {

    override val mViewModel: PostViewModel by viewModels()
    override val mPageCanBack: Boolean = false
    override val mLayoutResId: Int
        get() = R.layout.fragment_recommend

    private lateinit var mUiLoader: UILoader
    private lateinit var mContentBinding: LayoutRefreshRecyclerviewBinding
    companion object {
        private const val CATEGORY = "category"
        private const val KEYWORD = "keyword"
        private const val VEHICLE_MODEL_ID = "vehicle_model_id"
        private const val MEMBER_ID = "member_id"
        private const val SHOW_AUTHOR_LABEL = "SHOW_AUTHOR_LABEL"
        private const val IF_MY = "if_my"
        private const val IF_FOCUS = "IF_FOCUS"
        private const val IF_COLLECTION = "IF_COLLECTION"
        private const val SERIES = "series"
        fun newInstance(
            category: Int? = null,
            vehicleModelId: String? = null,
            series: String? = null,
            keyword: String? = null,
            memberId: String? = null,
            ifMy: Int? = null,
            ifFocus: Int? = null,
            ifCollection: Boolean = false,
            showAuthorLabel: Boolean = true
        ): PostListFragment {
            val args = Bundle()
            if (category != null)
                args.putInt(CATEGORY, category)
            args.putString(VEHICLE_MODEL_ID, vehicleModelId)
            args.putString(SERIES, series)
            args.putString(KEYWORD, keyword)
            args.putString(MEMBER_ID, memberId)
            args.putBoolean(SHOW_AUTHOR_LABEL, showAuthorLabel)
            args.putInt(IF_MY, ifMy ?: -1)
            args.putInt(IF_FOCUS, ifFocus ?: -1)
            args.putBoolean(IF_COLLECTION, ifCollection)
            val fragment = PostListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    //分页参数
    private var category: Int? = null
    private var vehicleModelId: String? = null
    private var series: String? = null
    private var keyword: String? = null
    private var memberId: String? = null
    private var ifMy: Int? = null
    private var ifFocus: Int? = null
    private var ifCollection = false
    private var showAuthorLabel: Boolean = true

    private var mSharePostId: String = ""

    private var mPagingJob: Job? = null
    override fun initView() {
        mContentBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.layout_refresh_recyclerview,
            null,
            false
        )

        //设置分页参数
        vehicleModelId = arguments?.getString(VEHICLE_MODEL_ID)
        series = arguments?.getString(SERIES)
        keyword = arguments?.getString(KEYWORD)
        category = arguments?.getInt(CATEGORY)
        memberId = arguments?.getString(MEMBER_ID)
        ifMy = if (arguments?.getInt(IF_MY) == -1) null else arguments?.getInt(IF_MY)
        ifFocus = if (arguments?.getInt(IF_FOCUS) == -1) null else arguments?.getInt(IF_FOCUS)
        ifCollection = arguments?.getBoolean(IF_COLLECTION) ?: false
        showAuthorLabel = arguments?.getBoolean(SHOW_AUTHOR_LABEL) ?: true
        initAdapter()
    }

    override fun initListener() {
        mBinding.layoutContent.addView(mContentBinding.root)
    }

    override fun initData() {
        // 非关注页面 直接加载帖子列表
        setPagingObserver(category, vehicleModelId, series, keyword, memberId, ifMy, ifFocus)
    }

    //重新配置分页参数
    private fun setPagingObserver(
        category: Int?,
        vehicleModelId: String?,
        series: String?,
        keyword: String?,
        memberId: String?,
        ifMy: Int?,
        ifFocus: Int?
    ) {
        if (mPagingJob?.isActive == true) {
            mPagingJob!!.cancel()
        }
        this.category = category
        this.vehicleModelId = vehicleModelId
        this.series = series
        this.keyword = keyword
        this.memberId = memberId
        this.ifMy = ifMy
        this.ifFocus = ifFocus
        mViewModel.setPageParam(
            category,
            vehicleModelId,
            series,
            keyword,
            memberId,
            ifMy = ifMy,
            ifFocus = ifFocus,
            ifCollection = ifCollection
        )
        //网络请求
        mPagingJob = mViewModel.postPageEvent2.observeWithLifecycle(this) {
            mPostPagingAdapter.submitData(it)
        }
    }

    //我的发布页面不再校验，可删除，进入详情 列表的适配器
    private val mPostPagingAdapter by lazy {
        PostPagingAdapter(
            onUserClick = {
                ToastUtils.showShort(context,"你好")
            },
            onImageClick = { _, child, pics, imageInfo ->
                ToastUtils.showShort(context,"你好")
                val f = PhotoViewerDialog(pics, imageInfo, child)
                f.showDialog(childFragmentManager)
            }

        )
    }

    private fun initAdapter() {
        mContentBinding.rv.layoutManager = LinearLayoutManager(context)
        //数据
        mContentBinding.rv.adapter = mPostPagingAdapter

    }
}